<?php
require_once('../recaptcha/recaptchalib.php');
require_once('../defensio/Defensio.php');
$missed_fields;
$oops = "Oops, problems, problems! ";
$you_need_to = "You need to";

function validateComment($name,$location,$message,$challenge,$response,$client,$recaptcha_key, $defensio_key) {
	try {
		$msg = null;
		//Validate for profanity check
		$fields = array($name, $location, $message);
		$msg = checkProfanity($fields,$defensio_key,$client);
		if ($msg == null) {
			//validate missing fields
			$fields = array("Name" => $name, "Location" => $location, "Message" => $message);
			validateMissingFields($fields);
			//validate comment
			$msg = validateMessage($message,$msg);
			//validate recaptcha
			$msg = validateReCaptcha($recaptcha_key,$client,$challenge,$response,$msg);
			//finally append missing fields it any
			$msg = processMissingFields($msg);
			if ($msg != null) $msg = "Oops, problems, problems! " . $msg;
		}
		return $msg;
	} catch (Exception $e) {
		return $e->getMessage();
	}
}
 
function validateContactMe($name,$replyTo,$subject,$message,$challenge,$response,$client,$recaptcha_key, $defensio_key) {
	try {
		$msg = null;
		//Validate for profanity check
		$fields = array($name, $replyTo, $subject, $message);
		$msg = checkProfanity($fields,$defensio_key,$client);
		if ($msg == null) {
			//validate missing fields
			$fields = array("Name" => $name, "Subject" => $subject, "Reply To" => $replyTo, "Message" => $message);
			validateMissingFields($fields);
			//validate replyTo
			$msg = validateEmail(trim($replyTo),$msg);
			//validate comment
			$msg = validateMessage($message,$msg);
			//validate recaptcha
			$msg = validateReCaptcha($recaptcha_key,$client,$challenge,$response,$msg);
			//finally append missing fields it any
			$msg = processMissingFields($msg);
			if ($msg != null) $msg = "Oops, problems, problems! " . $msg;
		}
		return $msg;
	} catch (Exception $e) {
		return $e->getMessage();
	}
}

function validateMessage($message, $msg) {
	$result = $msg;
	//check if more than 1000 characters
	if (strlen(trim($message)) > 1000) {
		if ($result != null)
			$result = $result . " Besides " . $GLOBALS['you_need_to'] . " shorten the message.";
		else
			$result = $GLOBALS['you_need_to'] . " shorten the message.";
	}
	return $result;
}

/* Validates recaptcha */
function validateReCaptcha($key,$client,$challenge,$response,$msg) {
	$ret = $msg;
	$resp = recaptcha_check_answer($key,$client,$challenge,$response);
	if (!$resp->is_valid) {
			if ($msg != null)
				$ret = $msg . " Oh! By the way, the ReCAPTCHA was not entered correctly.";
			else
				$ret = "The ReCAPTCHA was not entered correctly.";
	}
	return $ret;
}

/* Validates for profanity */
function checkProfanity($fields,$key,$client) {
	$ret = null;
	//defensio call here
	$d = new Defensio($key,$client);
	
	$result =  $d->postProfanityFilter($fields);
	if ($result[0] == 200 && strpos($result[1],"*") !== false) {
		$ret = "EEW!! What kind of language are these?";
	} 
	return $ret;
}

/* Validates email */
function validateEmail($email, $msg) {
  	$result = $msg;
  	if (strlen(trim($email)) > 0) {
		if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
			if ($result != null)
				$result = $result . ". And that's not your email id is it?'";
			else
				$result = "That's not your email id is it?";
		}
  	}
  	return $result;
}

/* Lists out all required fields that missing */
function  validateMissingFields($fields) {
		$GLOBALS['missed_fields'] = array();
		$arrayKeys = array_keys($fields);
		for ($cntr = 0; $cntr < count($arrayKeys); $cntr++) {
				if (strlen(trim($fields[$arrayKeys[$cntr]])) == 0) {
					$GLOBALS['missed_fields'][] = $arrayKeys[$cntr];
				}
		}
}

/* appends the missing fields message at the end of the message */
function processMissingFields($msg) {
		$ret = $msg;
		if (count($GLOBALS['missed_fields']) > 0) {
				if ($ret != null)
					$ret = $ret . " You also missed to enter ";
				else
					$ret = "You missed to enter ";
			for ($cntr = 0; $cntr < count($GLOBALS['missed_fields']); $cntr++) {
				if ($cntr == 0)
					$ret = $ret . $GLOBALS['missed_fields'][$cntr];
				else if ($cntr == (count($GLOBALS['missed_fields']) - 1))
					$ret = $ret . " and " . $GLOBALS['missed_fields'][$cntr];
				else
					$ret = $ret . ", " . $GLOBALS['missed_fields'][$cntr];
				if ($cntr == (count($GLOBALS['missed_fields']) - 1))
					$ret = $ret . ".";
			}
		}
		return $ret;
}
?>
